﻿#include "udpsender.h"
#include <QJsonDocument>

UdpSender::UdpSender(QObject *parent)
    : QObject{parent}
{
    sender = new QUdpSocket(this);
}

bool UdpSender::setIpAndPort(QString ip, int port)
{
    if(address.setAddress(ip)) {
        this->port = port;
        return true;
    } else {
        return false;
    }
}

void UdpSender::sendJson(QJsonObject obj)
{
    QJsonDocument jsonDoc;
    jsonDoc.setObject(obj);
    QByteArray byteArray = jsonDoc.toJson(QJsonDocument::Compact);
    //QByteArray byteArray = jsonDoc.toJson();
    sender->writeDatagram(byteArray.data(), byteArray.size(), address, port);
}
